//
//  ZhengViewController.m
//  selfLocation
//
//  Created by h92 on 15/1/7.
//  Copyright (c) 2015年 李腾飞. All rights reserved.
//

#import "ZhengViewController.h"

@interface ZhengViewController ()

@end

@implementation ZhengViewController
@synthesize textLabel;
@synthesize textTextField;

- (void)viewDidLoad {
    [super viewDidLoad];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*-------------------------------点击查询，实现搜索位置信息--------------------------------*/
- (IBAction)pressToFind:(id)sender {
    if(textTextField.text==nil||textTextField.text.length==0)
    {
        return ;
    }
    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    
    // 实现查询
    [geocoder geocodeAddressString:textTextField.text completionHandler:^(NSArray *placemarks, NSError *error){
        NSLog(@"查询记录%i",(int)[placemarks count]);
        if ([placemarks count]>0) {
            CLPlacemark *placemark=placemarks[0];
            
            // 保存坐标对象
            CLLocationCoordinate2D coordinate=placemark.location.coordinate;
            NSString *strCoordinate=[NSString stringWithFormat:@"经度:%3.5f \n纬度:%3.5f",coordinate.latitude,coordinate.longitude];
            
            // placemark 实力不俗
            NSDictionary *addressDictionary=placemark.addressDictionary;
            NSString *address=[addressDictionary objectForKey:(NSString*)kABPersonAddressStreetKey];
            NSString *state=[addressDictionary objectForKey:(NSString*)kABPersonAddressStateKey];
            NSString *city=[addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey];
            
            address=address==nil?@"":address;
            state=state==nil?@"":state;
            city=city==nil?@"":city;
            
            
            textLabel.text=[NSString stringWithFormat:@"%@ \n%@ \n%@ \n%@",strCoordinate,state,city,address];
            if ([address isEqualToString:@""]) {
                NSLog(@"为什么没有街道信息");
            }else
            {
                 NSLog(@"%@",address);
            }
           
            [textTextField resignFirstResponder];
        }
        
    }];
    
    
}
@end
